package cn.zyq.ykb.service.sys.impl;


import cn.zyq.ykb.beans.sys.SysAdmin;
import cn.zyq.ykb.beans.sys.SysPost;
import cn.zyq.ykb.mapper.sys.SysAdminPostMapper;
import cn.zyq.ykb.mapper.sys.SysPostMapper;
import cn.zyq.ykb.service.sys.SysPostService;
import cn.zyq.ykb.system.common.BaseService;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.ry.UserConstants;
import cn.zyq.ykb.system.ry.util.SecurityUtils;
import cn.zyq.ykb.system.ry.util.StringUtils;
import com.aliyun.oss.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 岗位信息表
 * 2023-05-12 20:14:09
 */

@Slf4j
@Service
public class SysPostServiceImpl extends BaseService implements SysPostService {
    @Autowired
    private SysPostMapper sysPostMapper;

    @Autowired
    private SysAdminPostMapper sysAdminPostMapper;

    @Override
    public PageInfo<SysPost> list(PageParam pageParam, SysAdmin admin) {
        PageHelper.startPage(pageParam.getPage(), pageParam.getPageSize());
        List<SysPost> dbList = this.sysPostMapper.list(pageParam.getKv());
        return new PageInfo<>(dbList);
    }

    @Override
    public void add(SysPost param, SysAdmin admin) {
        this.setCreateBaseData(param, admin);
        this.sysPostMapper.insertSelective(param);
    }

    @Override
    public void update(SysPost param, SysAdmin admin) {
        Integer id = param.getSysPostID();
        this.setUpdateBaseData(param, admin);
        this.sysPostMapper.updateByPrimaryKeySelective(param);
    }

    @Override
    public void del(String serviceID, SysAdmin admin) {
        SysPost dbInfo = this.detail(serviceID);
        Integer id = dbInfo.getSysPostID();
        this.deleteLogic(id, admin, this.sysPostMapper);
    }

    @Override
    public SysPost detail(String serviceID, SysAdmin admin) {
        return this.detail(serviceID);
    }

    @Override
    public SysPost detail(String serviceID) {
        SysPost dbInfo = this.getByServiceID(serviceID, SysPost.class, this.sysPostMapper);
        this.checkLogicExist(dbInfo);
        return dbInfo;
    }

    /**
     * 查询岗位信息集合
     *
     * @param param 岗位信息
     * @return 岗位信息集合
     */
    @Override
    public List<SysPost> selectPostList(PageParam param) {
        return sysPostMapper.list(param.getKv());
    }

    /**
     * 查询所有岗位
     *
     * @return 岗位列表
     */
    @Override
    public List<SysPost> selectPostAll() {
        return sysPostMapper.selectAll();
    }

    /**
     * 通过岗位ID查询岗位信息
     *
     * @param postId 岗位ID
     * @return 角色对象信息
     */
    @Override
    public SysPost selectPostById(Integer postId) {
        return sysPostMapper.selectByPrimaryKey(postId);
    }

    /**
     * 根据用户ID获取岗位选择框列表
     *
     * @param userId 用户ID
     * @return 选中岗位ID列表
     */
    @Override
    public List<Integer> selectPostListByUserId(Integer userId) {
        return sysPostMapper.selectPostListByUserId(userId);
    }

    /**
     * 校验岗位名称是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostNameUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getSysPostID()) ? -1 : post.getSysPostID();
        SysPost info = sysPostMapper.checkPostNameUnique(post.getPostName());
        if (StringUtils.isNotNull(info) && !info.getSysPostID().equals(postId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验岗位编码是否唯一
     *
     * @param post 岗位信息
     * @return 结果
     */
    @Override
    public boolean checkPostCodeUnique(SysPost post) {
        Integer postId = StringUtils.isNull(post.getSysPostID()) ? -1 : post.getSysPostID();
        SysPost info = sysPostMapper.checkPostCodeUnique(post.getPostCode());
        if (StringUtils.isNotNull(info) && !info.getSysPostID().equals(postId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 通过岗位ID查询岗位使用数量
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int countUserPostById(Integer postId) {
        return sysAdminPostMapper.countUserPostById(postId);
    }

    /**
     * 删除岗位信息
     *
     * @param postId 岗位ID
     * @return 结果
     */
    @Override
    public int deletePostById(Integer postId) {
        return sysPostMapper.deleteByPrimaryKey(postId);
    }

    /**
     * 批量删除岗位信息
     *
     * @param postIds 需要删除的岗位ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deletePostByIds(Integer[] postIds) {
        for (Integer postId : postIds) {
            SysPost post = selectPostById(postId);
            if (countUserPostById(postId) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", post.getPostName()));
            }
        }
        int i = 0;
        for (Integer postId : postIds) {
            sysPostMapper.deleteByPrimaryKey(postIds);
            i++;
        }
        return i;
    }

    /**
     * 新增保存岗位信息
     *
     * @param param 岗位信息
     * @return 结果
     */
    @Override
    public int insertPost(SysPost param) {
        this.setCreateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.sysPostMapper.insertSelective(param);
    }

    /**
     * 修改保存岗位信息
     *
     * @param param 岗位信息
     * @return 结果
     */
    @Override
    public int updatePost(SysPost param) {
        this.setUpdateBaseData(param, SecurityUtils.getLoginUser().getSysAdmin());
        return this.sysPostMapper.updateByPrimaryKeySelective(param);
    }
}
